package com.hfh.review;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author hfh
 * @Date 2022 09 16 09 24
 **/
public class LockDemo implements Runnable{
    volatile int num = 100;
    private final ReentrantLock lock = new ReentrantLock();//创建锁

    public static void main(String[] args) {
        LockDemo ld = new LockDemo();
        for (int i = 1;i<=5;i++){
            new Thread(ld,"窗口"+i+":").start();
        }
    }

    @Override
    public void run() {
        while (num>0){
            lock.lock();
            try{
                if (num>0){
                    System.out.println(Thread.currentThread().getName()+"卖了第"+num--+"张票");
                }
            } finally {
                lock.unlock();
            }
        }
    }
}
